package com.nidu.demo.role.executor.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.role.convertor.RoleConvertor;
import com.nidu.demo.role.convertor.RoleCriteriaConvertor;
import com.nidu.demo.role.dto.RoleCO;
import com.nidu.demo.role.dto.RoleListQry;
import com.nidu.demo.role.gateway.RoleGateway;
import com.nidu.demo.role.model.Role;
import com.nidu.demo.role.model.RoleCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 角色列表查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class RoleListQryExe {

    private final RoleGateway gateway;

    private final RoleConvertor convertor;

    private final RoleCriteriaConvertor criteriaConvertor;

    public MultiResponse<RoleCO> execute(RoleListQry qry) {
        RoleCriteria criteria = criteriaConvertor.toDataObject(qry);
        List<Role> roles = gateway.listByCondition(criteria);
        if(CollUtil.isEmpty(roles)){
            throw new BizException(ErrorCodeConstants.ROLE_NOT_FOUND.getCode(), ErrorCodeConstants.ROLE_NOT_FOUND.getMessage());
        }
        List<RoleCO> roleCOS = convertor.toClientObjects(roles);
        return MultiResponse.of(roleCOS);
    }

}